package com.workcell.workdata.im.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.workcell.workdata.im.entity.*;
import com.workcell.workdata.im.repository.*;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.model.vo.ChatVO;
import com.workcell.workdata.im.repository.*;
import com.workcell.workdata.im.service.ChatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ChatServiceImpl implements ChatService {

    @Resource
    private ChatRepository chatRepository;
    @Resource
    private GroupMessageRepository groupMessageRepository;

    @Resource
    private ChatMessageRepository chatMessageRepository;

    @Resource
    private GroupMemberRepository groupMemberRepository;

    @Resource
    private GroupRepository groupRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private EventMessageRepository eventMessageRepository;


    @Override
    public void save(Chat chat) {
        Chat chatData = chatRepository.findByType(chat.getAction(), chat.getUid(), chat.getChatId());
        if(null == chatData){
            chat.setType("0");
            chat.setTop("0");
            chat.setState("0");
            chat.setCreateTime(new Date());
            chatRepository.save(chat);
        }else{
            if (StringUtils.equals(chatData.getState(),"1")){
                chatData.setState("0");
                chatData.setUpdateTime(new Date());
                chatRepository.saveAndFlush(chatData);
            }
        }
    }

    @Override
    public List<ChatVO> getList(Long uid) {
        List<Chat> listChat= chatRepository.findListByUid(uid);
        List<ChatVO> collect = listChat.stream().map(chat -> {
            ChatVO chatVO = new ChatVO();
            BeanUtils.copyProperties(chat, chatVO);
            List<Map<String, Object>> maps = new ArrayList<>();
            switch (chat.getAction()) {
                case MessageAction.ACTION_0:
                    maps = this.ChatMessageList(chat.getUid(),chat.getChatId());
                    chatVO.setInfo(this.findUserInfo(chat.getChatId()));
                    break;
                case MessageAction.ACTION_3:
                    maps = this.groupMessageList(chat.getChatId());
                    chatVO.setGroups(this.findGroupMemberList(chat.getChatId()));
                    chatVO.setInfo(this.findGroupInfo(chat.getChatId()));
                    break;
            }
            chatVO.setMsgList(maps);
            return chatVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> groupMessageList(Long chatId) {
        List<GroupMessage> groupMessages = groupMessageRepository.findListByGroupId(chatId);
        List<Map<String, Object>> collect = groupMessages.stream().map(groupMessage -> {
            Map<String, Object> stringObjectMap = BeanUtil.beanToMap(groupMessage);
            return stringObjectMap;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> ChatMessageList(long sender, long receiver) {
        List<ChatMessage> chatMessages = chatMessageRepository.findChatMessage(sender, receiver);
        List<Map<String, Object>> collect = chatMessages.stream().map(chatMessage -> {
            Map<String, Object> stringObjectMap = BeanUtil.beanToMap(chatMessage);
            return stringObjectMap;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> findGroupMemberList(Long groupId) {
        List<GroupMember> groupMembers = groupMemberRepository.findListBygroupId(groupId);
        List<Map<String, Object>> collect = groupMembers.stream().map(groupMember -> {
            Map<String, Object> stringObjectMap = BeanUtil.beanToMap(groupMember);
            String name = userRepository.findName(groupMember.getUid());
            stringObjectMap.put("name",name);
            return stringObjectMap;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Map<String, Object> findGroupInfo(Long chatId) {
        Group groupById = groupRepository.findGroupById(chatId);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(groupById);
        return stringObjectMap;
    }

    @Override
    public Map<String, Object> findUserInfo(Long id) {
        User newUser = userRepository.findById(id).orElse(null);
        if(ObjectUtil.isNull(newUser)){
            return null;
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(newUser);
        stringObjectMap.remove("password");
        return stringObjectMap;
    }

    @Override
    public void update(Chat chat) {
        Chat ct = chatRepository.findChatById(chat.getId());
        if(StringUtils.isNotBlank(chat.getState())){
            ct.setState(chat.getState());
        }
        if(StringUtils.isNotBlank(chat.getTop())){
            ct.setTop(chat.getTop());
            ct.setTopTime(new Date());
        }
        if(StringUtils.isNotBlank(chat.getType())){
            ct.setType(chat.getType());
        }
        ct.setUpdateTime(new Date());
        chatRepository.saveAndFlush(ct);
    }

    @Override
    public List<Message> groupMessage(long groupId) {
        List<Message> messageList = new LinkedList<>(eventMessageRepository.findActionBySender(groupId));
        messageList.addAll(groupMessageRepository.findMessageById(groupId));
        messageList.sort(Comparator.comparingLong(o -> o.getCreateTime().getTime()));
        return messageList;
    }


}
